package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.ApartmentInfoService;
import com.atguigu.lease.web.app.service.RoomInfoService;
import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.app.vo.attr.AttrValueVo;
import com.atguigu.lease.web.app.vo.fee.FeeValueVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.atguigu.lease.web.app.vo.room.RoomDetailVo;
import com.atguigu.lease.web.app.vo.room.RoomItemVo;
import com.atguigu.lease.web.app.vo.room.RoomQueryVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
@Slf4j
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo> implements RoomInfoService {

    @Autowired
    private RoomInfoMapper roomInfoMapper;

    @Autowired
    private GraphInfoMapper graphInfoMapper;

    @Autowired
    private LeaseTermMapper leaseTermMapper;

    @Autowired
    private AttrValueMapper attrValueMapper;

    @Autowired
    private RoomFacilityMapper roomFacilityMapper;
    @Autowired
    private RoomLabelMapper roomLabelMapper;
    @Autowired
    private PaymentTypeMapper paymentTypeMapper;
    @Autowired
    private FeeValueMapper feeValueMapper;
    @Autowired
    private ApartmentInfoService apartmentInfoService;
    public IPage<RoomItemVo> pageRoomItemByQuery(Page<RoomItemVo> roomItemVoPage, RoomQueryVo queryVo) {
       return roomInfoMapper.pageRoomItemByQuery(roomItemVoPage, queryVo);

    }

    @Override
    public RoomDetailVo getDetailById(Long id) {
         RoomInfo roomInfo =roomInfoMapper.selectById(id);
         if(roomInfo == null){
             return null;
         }
         //2.查询图片
        List<GraphVo> graphVoList = graphInfoMapper.selectListByItemAndId(ItemType.ROOM,id);

         //3.查询属性列表
        List<AttrValueVo> attrValueVoList = attrValueMapper.selectAttrValueVoListByRoomId(id);

        //4.查询配套信息
        List<FacilityInfo> facilityInfoList = roomFacilityMapper.selectFacilityInfoListByRoomId(id);

        //5.查询标签信息
        List<LabelInfo> roomLabelList = roomLabelMapper.selectLabelInfoListByRoomId(id);

        //6.查询支付方式
        List<PaymentType> paymentTypeList = paymentTypeMapper.selectListByRoomId(id);

        //7.查询杂费列表
       List<FeeValueVo> feeValueVoList = feeValueMapper.selectFeeValueVoListByApartmentId(roomInfo.getApartmentId());

        //8.查询租期
        List<LeaseTerm> leaseTermList = leaseTermMapper.selectListByRoomId(id);

        //9.查询所属公寓信息
        ApartmentItemVo apartmentItemVo = apartmentInfoService.selectApartmentItemVoById(roomInfo.getApartmentId());



        //3.封装返回
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        BeanUtils.copyProperties(roomInfo,roomDetailVo);


        roomDetailVo.setApartmentItemVo(apartmentItemVo);
        roomDetailVo.setGraphVoList(graphVoList);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        roomDetailVo.setLabelInfoList(roomLabelList);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        roomDetailVo.setFeeValueVoList(feeValueVoList);
        roomDetailVo.setLeaseTermList(leaseTermList);

        //4.封装返回
        return  roomDetailVo;
    }

    /**
     * 根据公寓id分页查询房间列表
     * @param roomItemVoPage
     * @param id
     * @return
     */
    @Override
    public IPage<RoomItemVo> pageItemByAparmentId(IPage<RoomItemVo> roomItemVoPage, Long id) {

        return roomInfoMapper.pageItemByApartmentId(roomItemVoPage, id);
    }
}




